package com.cskaoyan.wordmemorize.converter;

import com.cskaoyan.wordmemorize.dao.entity.EmployeeDO;
import com.cskaoyan.wordmemorize.dao.entity.RoleDO;
import com.cskaoyan.wordmemorize.dto.admin.DeleteEmployeeRoleOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.EmployeeDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.RoleDTO;
import com.cskaoyan.wordmemorize.request.EmployeeCommand;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.Named;

import java.util.List;

@Mapper(componentModel = "spring")
public interface EmployeeConverter {


    @Mapping(target = "password", ignore = true)
    EmployeeDTO employeeDO2DTO(EmployeeDO employeeDO);

    @Mapping(source = "employeeList", target = "pageList")
    @Mapping(source = "total", target = "totalCount")
    PageDTO<EmployeeDTO> emoloyeePage2PageDTO(List<EmployeeDO> employeeList, Long total);

    @Mappings({
            @Mapping(source = "id", target = "id", qualifiedByName = "stringToLong"),
            @Mapping(source = "name", target = "name"),
            @Mapping(source = "nickName", target = "nickName"),
            @Mapping(source = "phoneNumber", target = "phoneNumber"),
            @Mapping(source = "password", target = "password"),
            @Mapping(source = "avatar", target = "avatar")
    })
    EmployeeDO command2EmployeeDO(EmployeeCommand command);

    DeleteEmployeeRoleOperateDTO toDeleteEmployeeRoleOperateDTO(EmployeeDO employeeDO, RoleDO roleDO);

    @Named("stringToLong")
    default Long stringToLong(String id) {
        if (id == null || id.isEmpty())
            return null;
        return Long.valueOf(id);
    }
}
